package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.pojo.ExtReturn;
import com.ld.hpfs.pojo.User;
import com.ld.hpfs.service.MailTemplateService;
import com.ld.hpfs.web.interseptor.WebConstants;

@Controller
public class MailTemplateController {

	private static final Logger logger = LoggerFactory
			.getLogger(MailTemplateController.class);

	@Autowired
	private MailTemplateService mailTemplateService;

	/**
	 * 更新模板
	 */
	@RequestMapping(value = "/updateTemplate")
	@ResponseBody
	public Object updateTemplate(HttpSession session,
			HttpServletRequest request) {
		User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
		if (user == null) {
			logger.error("未登陆!");
			return new ExtReturn(false, "未登陆!");
		}
		String companyId = session.getAttribute(WebConstants.CURRENT_COMPANY).toString();
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("companyId", companyId);
		// 模版类型
		param.put("templateType", request.getParameter("templateType"));
		// 创建者
		param.put("creator", user.getUserId());		

		// 查询模板
		HashMap<String, Object> templateList = mailTemplateService
				.getMailTemplate(param);

		int iResult = 9;

		// 如果不存在对应Type的模板，则添加模板
		if (templateList == null || templateList.keySet().size() == 0) {
			iResult = mailTemplateService.addMailTemplate(param);

			if (iResult != 1) {
				logger.error("添加邮件、短信模板失败");
				return new ExtReturn(false, "false");
			}
		}
		// 收件人
		param.put("receiver", request.getParameter("receiver"));
		
		// 抄送
		param.put("carbonCopy", request.getParameter("carbonCopy"));
		
		// 隐藏抄送
		param.put("blindCarbonCopy", request.getParameter("blindCarbonCopy"));
		
		// 主题
		param.put("theme", request.getParameter("theme"));
		
		// 邮件正文
		param.put("mailContent", request.getParameter("mailContent"));
		
		// 短信正文
		param.put("messageContent", request.getParameter("messageContent"));
		
		// 邮件启用Flag
		param.put("mailNoticeFlg", request.getParameter("mailNoticeFlg"));
		
		// 短信启用Flag
		param.put("messageNoticeFlg", request.getParameter("messageNoticeFlg"));

		// 更新模板
		iResult = mailTemplateService.updateMailTemplate(param);

		if (iResult != 1) {
			logger.error("更新邮件、短信模板失败");
			return new ExtReturn(false, "更新模板失败！");
		}
		return new ExtReturn(true, "更新模板成功！");
	}

	/**
	 * 查询模板
	 */
	@RequestMapping(value = "/getTemplate")
	@ResponseBody
	public void getTemplate(
			HttpSession session, HttpServletRequest request,
			HttpServletResponse response, PrintWriter writer) {
		User user = (User) session.getAttribute(WebConstants.CURRENT_USER);
		if (user == null) {
			return;
		}
		HashMap<String, Object> param = new HashMap<String, Object>();
		
		String companyId = session.getAttribute(WebConstants.CURRENT_COMPANY).toString();
		// 根据模板类型查询模板
		param.put("templateType", request.getParameter("templateType"));
		param.put("companyId", companyId);
		
		// 查询模板
		HashMap<String, Object> templateList = mailTemplateService
				.getMailTemplate(param);
		//logger.debug("查询邮件、短信模板");

		response.setContentType("text/plain;charset=UTF-8");
		String resultstring = JackJson.fromObjectToJson(templateList);
		logger.debug("{}", resultstring);
		writer.print(resultstring);
	}
}
